package com.magnusart.scrumppm.presenters;

import static com.magnusart.scrumppm.model.States.Transition.DETAILS_TO_CHOOSER;

import java.util.Observable;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Text;

import com.magnusart.scrumify.dao.CommonDAO;
import com.magnusart.scrumppm.gui.ProjectDetails;
import com.magnusart.scrumppm.model.Project;
import com.magnusart.scrumppm.model.ProjectBuilder;
import com.magnusart.scrumppm.model.States.Transition;
import com.magnusart.scrumppm.presenters.GlobalEvents.Events;

public class ProjectDetailsPresenter extends AbstractPresenter<ProjectDetails, CommonDAO<Project, Project, ProjectBuilder>>
{

	private Project currentProject;
	
	
	public ProjectDetailsPresenter( ProjectDetails view, CommonDAO<Project, Project, ProjectBuilder> projectDao ) {
		super( view, projectDao );
		
		addListeners();
		initView();
	}
	/* (non-Javadoc)
	 * @see com.magnusart.scrumppm.presenters.AbstractPresenter#addListeners()
	 */
	@Override
	protected void addListeners() {
		GlobalEvents.observe().addObserver( this );
		view.getDeleteProjectButton().addListener( SWT.Selection, this );
		view.getDetailsArea().addListener( SWT.FocusIn, this );
		view.getDetailsArea().addListener( SWT.FocusOut, this );
		view.getProjectName().addListener( SWT.FocusIn, this );
		view.getProjectName().addListener( SWT.FocusOut, this );
	}
	
	@Override
	protected void initView() {
		// TODO Ska denna verkligen vara med i abstractpresenter?
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
	 */
	/*
	 * Do not forget to add an event listener for each case!
	 */
	@Override
	public void handleEvent( Event e ) {
		if( e.widget.getData() instanceof WidgetTypes ) {
			WidgetTypes s = (WidgetTypes)( e.widget.getData() );
			switch( s ) {
				case DeleteProjectButton:
					doDeleteProject();
					break;
				case DetailsAreaSelection:
					if ( e.type == SWT.FocusIn )
						doEnableEdit( (Text)e.widget );
					else if( e.type == SWT.FocusOut )
						doDisableEdit( (Text)e.widget );
					break;
				case ProjectNameSelection:
					if ( e.type == SWT.FocusIn )
						doEnableEdit( (Text)e.widget );
					else if( e.type == SWT.FocusOut )
						doDisableEdit( (Text)e.widget );
					break;
			}
		}
	}


	@Override
	public void update( Observable obs, Object obj ) {
		if( obs.equals(  GlobalEvents.observe() ) && obj instanceof GlobalEvents.Events) {
			Events change = (Events) obj;
			switch( change ) {
			case UPDATE_CURRENT_PROJECT:
				currentProject = GlobalEvents.getActiveProject();
				updateDetails();
				break;
			}
		}
	}

	private void updateDetails() {
		if( currentProject != null ) {
			Text projectName = view.getProjectName();
			projectName.setText( currentProject.getName() );
			
			Text detailsArea = view.getDetailsArea();
			detailsArea.setText( currentProject.getDescription() );
			
			view.layout(); // Needed to make the changes persistent.	
		}
	}
	
	
	/**
	 * Goes back to previous state
	 */
	private void doGoBack() {
		Transition.to( DETAILS_TO_CHOOSER );	
	}	
	

	/**
	 * Deletes the current project
	 */
	private void doDeleteProject() {
		dao.delete( currentProject );
		currentProject = null;
		GlobalEvents.refreshProjectListings();
		doGoBack();
	}
	

	private void doEnableEdit(Text widget) {
		widget.setEditable( true );
		
	}
	

	private void doDisableEdit(Text widget) {
		widget.setEditable( false );	
	}
}
